package ShareYourWay.webpages;

import org.apache.commons.collections.map.StaticBucketMap;
import org.apache.wicket.Session;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.protocol.http.WebSession;

import ShareYourWay.functions.App;
import ShareYourWay.functions.SignInSession;
import ShareYourWay.project.User;
import ShareYourWay.mail.Gmail;



public class HomePage extends WebPage {
	private static final long serialVersionUID = 1L;

	public HomePage() {
		super();
		
		final User u =  new User();
		final User user =  new User();
		final Gmail g = new Gmail();
				
		Form<User> formlogin = new Form<User>("form-login");	
		
		final App app = new App ();
		
				
		TextField<String> temail = new TextField<String>("email", new PropertyModel<String>(u, "mail"));
		temail.setRequired(true);
		
		PasswordTextField tpass = new PasswordTextField("pass", new PropertyModel<String>(u, "password"));
		tpass.setRequired(true);
		
        AjaxButton buttonsignin = new AjaxButton("signin") {

			private static final long serialVersionUID = 1L;

			@Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				  
//			    System.out.println("session  1111111111111111111111");
			    Session s1 = Session.get();
			    
			   // User uu = s1.getAttribute("");
			    //s1.bind();

				AuthenticatedWebSession session = AuthenticatedWebSession.get();

			    System.out.println("session "+session.getClass());
				
				if(session.signIn(u.getMail(), u.getPassword())) {    				
					setResponsePage(Meets.class);
				}else
					target.appendJavaScript("alert('Email o pass incorrecto');");
            }
        }; // Fin boton Sign in Ajax

    	add(formlogin);
    	formlogin.add(temail);
    	formlogin.add(tpass);
    	formlogin.add(buttonsignin);
    	
   
    	Form <User> FormRegister = new Form <User> ("form-register");
		
		TextField<String> tname = new TextField<String>("tname", new PropertyModel<String>(user, "name"));
		tname.setRequired(true);
		
		TextField<String> tsurname = new TextField<String>("tsurname", new PropertyModel<String>(user, "surname"));
		tsurname.setRequired(true);
		
		TextField<String> tpassword = new TextField<String>("tpassword", new PropertyModel<String>(user, "password"));
		tpassword.setRequired(true);
		
		TextField<String> tnick = new TextField<String>("tnick", new PropertyModel<String>(user, "nick"));
		tnick.setRequired(true);
		TextField<String> tmail = new TextField<String>("tmail", new PropertyModel<String>(user, "mail"));
		tmail.setRequired(true);
		
		
		 AjaxButton buttonsaveUser = new AjaxButton("saveuser") {
			private static final long serialVersionUID = 1L;

			@Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {

				User usr = new User (user); //Hago una copia del objeto, si no lo hago se sobreescribirá.
				
				// ID aleatorio (Provisional).
				//usr.setId(3);
				System.out.println("Estoy en registro");
				System.out.println(usr.getName());
				
				app.addUser(usr);
				
				//Enviem mail de confirmació
				String []mail = new String[]{usr.getMail()};
				g.sendRegisterMail(mail, usr.getName(), usr.getPassword());
				
				//Fem el login directament amb lusuari acabat de crear
				AuthenticatedWebSession sessionreg = AuthenticatedWebSession.get();

			    System.out.println("session "+sessionreg.getClass());
				
				if(sessionreg.signIn(usr.getMail(), usr.getPassword())) {    				
					setResponsePage(Meets.class);
				}else
					target.appendJavaScript("bootbox.alert('Email o pass incorrecto');");
//    					app.Listar();
//    					target.add(MeetPanel);
//    					target.appendJavaScript("cerrar_modal(); alertas('Evento creado');");

            }

        };
	        
        add(FormRegister);
        FormRegister.add(tname);
        FormRegister.add(tsurname);
        FormRegister.add(tpassword);
        FormRegister.add(tmail);
        FormRegister.add(tnick);
        FormRegister.add(buttonsaveUser);
       
    }
}
